// pages/course/course.js - 严格按照test示例设计规范
Page({
  data: {
    searchKeyword: '',
    goals: ['放松舒缓', '稳定强化', '活动恢复', '姿态调整', '动作控制', '预防训练'],
    activeGoal: 0,
    courses: [
      {
        id: 1,
        title: '肩颈舒缓放松·初级',
        image: 'https://design.gemcoder.com/staticResource/echoAiSystemImages/c6028ae9aff08b3818e5c6d448c37e81.png',
        duration: '15分钟',
        part: '颈肩',
        goal: '放松',
        level: '初级',
        rating: 4.8,
        students: 1200,
        stars: ['★', '★', '★', '★', '☆']
      },
      {
        id: 2,
        title: '腰背舒缓·久坐人群适用',
        image: 'https://design.gemcoder.com/staticResource/echoAiSystemImages/72a798a011746ce7e5bf5434d1a7f226.png',
        duration: '20分钟',
        part: '腰背',
        goal: '放松',
        level: '中级',
        rating: 4.2,
        students: 896,
        stars: ['★', '★', '★', '★', '☆']
      }
    ]
  },

  onLoad(options) {
    console.log('课程分类页面加载完成', options);
    // 根据传入参数设置筛选条件
    if (options.type) {
      this.setData({
        activeGoal: this.getGoalIndex(options.type)
      });
    }
  },

  // 搜索输入
  onSearchInput(e) {
    this.setData({
      searchKeyword: e.detail.value
    });
    // 这里可以添加搜索逻辑
    this.searchCourses();
  },

  // 选择功能目标
  selectGoal(e) {
    const index = e.currentTarget.dataset.index;
    this.setData({
      activeGoal: index
    });
    // 根据目标筛选课程
    this.filterCoursesByGoal(index);
  },

  // 跳转到身体部位课程
  goToBodyPart(e) {
    const part = e.currentTarget.dataset.part;
    wx.navigateTo({
      url: `/pages/course/course?part=${part}`
    });
  },

  // 跳转到课程详情
  goToCourseDetail(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/detail/detail?type=course&id=${id}`
    });
  },

  // 查看更多课程
  goToMoreCourses() {
    wx.navigateTo({
      url: '/pages/course/course?type=all'
    });
  },

  // 搜索课程
  searchCourses() {
    const keyword = this.data.searchKeyword;
    if (!keyword) {
      this.loadAllCourses();
      return;
    }
    
    // 模拟搜索逻辑
    const filteredCourses = this.data.courses.filter(course => 
      course.title.includes(keyword) || 
      course.part.includes(keyword) || 
      course.goal.includes(keyword)
    );
    
    this.setData({
      courses: filteredCourses
    });
  },

  // 根据目标筛选课程
  filterCoursesByGoal(goalIndex) {
    const goal = this.data.goals[goalIndex];
    // 模拟筛选逻辑
    const filteredCourses = this.data.courses.filter(course => 
      course.goal === goal || goal === '放松舒缓'
    );
    
    this.setData({
      courses: filteredCourses
    });
  },

  // 加载所有课程
  loadAllCourses() {
    // 这里可以从服务器加载数据
    this.setData({
      courses: this.data.courses
    });
  },

  // 获取目标索引
  getGoalIndex(type) {
    const goalMap = {
      'relax': 0,
      'stable': 1,
      'recovery': 2,
      'posture': 3,
      'control': 4,
      'prevent': 5
    };
    return goalMap[type] || 0;
  }
});
